﻿using System;
using ASPNETVN.PORTAL.Components.Helpers;
using ASPNETVN.PORTAL.Components.UI;
using System.Web.UI;
using System.Data;
using ASPNETVN.PORTAL.Modules.GUIDE.Components;
using ASPNETVN.PORTAL.Components.Security;
using ASPNETVN.PORTAL.Components.Configuration.Settings;
using ASPNETVN.PORTAL.Components.UI.DataTypes;
using System.Web;
using ASPNETVN.PORTAL.Components.Share;
using System.Collections;
using System.Web.UI.WebControls;
namespace ASPNETVN.PORTAL.Modules.GUIDE.Controls
{
    public partial class GuideList : ModulePage
    {
        protected void Page_Load(object sender, EventArgs e)
        {

            if (IsPostBack == false)
            {
                ViewState["dsGuide"] = null;

                LoadData();
            }

        }
        public void LoadData()
        {
            try
            {
                //bind dropdown

                cbGuider.DataSource = Components.Guider.SelectAll(this.ModuleID);
                cbGuider.DataBind();
                cbGuider.Items.Insert(0, new ListItem("---Chọn cơ quan thực hiện---", "0"));

                ASPNETVN.PORTAL.Components.Share.Category.BuildDropDownListControlForAllID(ref cbCategory, this.ModuleID);
                cbCategory.Items.Insert(0, new ListItem("---Chọn lĩnh vực---", "0"));

                if (Request.QueryString["CategoryID"] != null)
                {
                    cbCategory.SelectedValue = Request.QueryString["categoryid"];                    
                }
                DataSet ds = Components.Article.SelectArticles_ByModuleID(this.ModuleID, Int32.Parse(cbCategory.SelectedValue), Int32.Parse(cbGuider.SelectedValue), "%" + txtTitle.Text.Trim() + "%");
               
                gvGuideContent.DataSource = ds;
                gvGuideContent.DataBind();
                ViewState["dsGuide"] = ds;

                int itemCount = Components.Article.GetArticleCount(this.ModuleID, Int32.Parse(cbCategory.SelectedValue), Int32.Parse(cbGuider.SelectedValue));
                TotalRecordLabel.Text = itemCount.ToString();
            }
            catch { }
        }
        protected void btnSearchGuider_Click(object sender, EventArgs e)
        {
            string value = txtTitle.Text.Trim();
            if (txtTitle.Text.IndexOf("%") < 0)
            {
                DataSet ds = Components.Article.SelectArticles_ByModuleID(this.ModuleID, 0, Int32.Parse(cbGuider.SelectedValue), "%" + value + "%");
                gvGuideContent.DataSource = ds;
                gvGuideContent.DataBind();
                if (Int32.Parse(cbCategory.SelectedValue) != 0)
                {
                    cbCategory.SelectedValue = "0";
                }
                ViewState["dsGuide"] = ds;

                int itemCount = Components.Article.GetArticleCount(this.ModuleID, Int32.Parse(cbCategory.SelectedValue), Int32.Parse(cbGuider.SelectedValue));
                TotalRecordLabel.Text = itemCount.ToString();
            }
            else
            {
                this.ShowMessage("Thông báo", "Không chấp nhận các ký tự đặc biệt");
            }
        }
        protected void btnSearchArea_Click(object sender, EventArgs e)
        {
            string value = txtTitle.Text.Trim();
            if (txtTitle.Text.IndexOf("%") < 0)
            {
                DataSet ds = Components.Article.SelectArticles_ByModuleID(this.ModuleID, Int32.Parse(cbCategory.SelectedValue), 0, "%" + value + "%");
                gvGuideContent.DataSource = ds;
                gvGuideContent.DataBind();
                if (Int32.Parse(cbGuider.SelectedValue) != 0)
                {
                    cbGuider.SelectedValue = "0";
                }
                ViewState["dsGuide"] = ds;

                int itemCount = Components.Article.GetArticleCount(this.ModuleID, Int32.Parse(cbCategory.SelectedValue), Int32.Parse(cbGuider.SelectedValue));
                TotalRecordLabel.Text = itemCount.ToString();
            }
            else
            {
                this.ShowMessage("Thông báo", "Không chấp nhận các ký tự đặc biệt");
            }

        }

        protected void gvGuideContent_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            gvGuideContent.PageIndex = e.NewPageIndex;
            if ((DataSet)ViewState["dsGuide"] != null)
            {
                gvGuideContent.DataSource = (DataSet)ViewState["dsGuide"];
                gvGuideContent.DataBind();
            }
            else
                LoadData();
        }
    }
}